package com.song.JavaThread_ch3_3;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class PrintQueue {
	
	private boolean freePrinters[];
	
	private Lock lockPrinters;
	
	private Semaphore semaphore;
	
	public PrintQueue() {
		super();
		semaphore = new Semaphore(3);
		freePrinters = new boolean[3];
		for(int i=0;i<3 ;i++)
			freePrinters[i] =true;
		lockPrinters = new ReentrantLock();
	}

	public void printJob(Object object) {
		
		try {
			semaphore.acquire();
			int assignedprinter =getPrinter();
			long duration = (long)(Math.random()*10);
			System.out.printf("%s : print a job in printer %d duration %d  \n" ,Thread.currentThread().getName(),assignedprinter,duration);
			TimeUnit.SECONDS.sleep(duration);
			freePrinters[assignedprinter]=true;
			
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			semaphore.release();
		}
		
	}

	private int getPrinter() {
		int ret =-1;
		try {
			lockPrinters.lock();
			
			for(int i=0 ;i<freePrinters.length;i++)
			{
				if(freePrinters[i]){
					ret = i;
					freePrinters[i] =false;
					break;
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			lockPrinters.unlock();
		}
		return ret;
	}
	
	
}
